package com.mercycrazy.recruit.controller;

import com.mercycrazy.recruit.user.UserLoginInfo;
import com.mercycrazy.recruit.service.IUserAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 对 SAAS 企业端暴露的鉴权微服务的相关接口
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-17
 */
@RestController
@RequestMapping("/passport/saas")
@Slf4j
@Tag(name = "SAAS企业端鉴权")
public class SaasPassportController {

    private final IUserAuthService userAuthService;

    public SaasPassportController(IUserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Operation(summary = "获取扫码登录二维码令牌",
            description = "获取扫码登录二维码登录令牌, 5分钟内有效, 不需要鉴权", method = "GET")
    @GetMapping("/qr-token")
    public String getQrToken() {
        return userAuthService.generateQrToken();
    }

    @Operation(summary = "查询二维码是否已经被扫码",
            description = "查询二维码是否已经被扫码, 前端定时轮询, 不需要鉴权", method = "GET")
    @GetMapping("/qr-token-read")
    public Boolean qrTokenHasBeenScan(@RequestParam String qrToken) {
        return userAuthService.qrTokenHasBeenScan(qrToken);
    }

    @Operation(summary = "网页端定时尝试登录",
            description = "网页端定时尝试登录, 未登录则返回空的登录信息, 不需要鉴权", method = "GET")
    @GetMapping("/check-login")
    public UserLoginInfo checkLogin(@RequestParam String qrToken) {
        return userAuthService.checkLogin(qrToken);
    }
}
